import storeApi from '../../api/store'
const QQMapWX = require('../../utils/qqmap-wx-jssdk.min.js');
const key = '6HOBZ-KJKC4-IKJUP-X7HJL-XODEQ-67BIA'


Page({
	data: {
		latitude: 0,
		longitude: 0,
		markers: [{
			id: 1,
			title: '实力位置',
			latitude: 21.64109,
			longitude: 110.91879,
			iconPath: '../../assets/images/marker.png',
			width: '55rpx',
			height: '69rpx'
		}],
		storeList: [],
		dict: {
			"OPENING": "营业中",
			"CLOSED": "已关店",
		}
	},
	mapContext: null,
	mapSdk: null,
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad(options) {
		this.loadCurrentLocation()
		this.initMapContext()
		this.initMapSdk()
		this.fetchStoreList()
	},
	initMapSdk() {
		this.mapSdk = new QQMapWX({key})
	},
	fetchStoreList() {
		storeApi.list().then(res => {
			const storeList = this.makeStoreList(res.data)
			// this.setData({
			// 	storeList
			// })
		})
	},
	makeStoreList(data) {
		let storeList = []

		const locationList = data.map(item => {
			return {
				latitude: item.latitude,
				longitude: item.longitude,
			}
		})
		this.mapSdk.calculateDistance({
			// from: {
			//   latitude: this.data.latitude,
			//   longitude: this.data.longitude,
			// },
			to: locationList,
			success: (res) => {
			//   storeList.forEach((item, key)=>{
			// 	storeList[key]['distance'] = (res.result.elements[key].distance/1000).toFixed(2)
			//   })
	  
			//   this.setData({
			// 	storeList
			//   })
			console.log(res);
			}
		  })
		return storeList
	},
	initMapContext() {
		wx.createSelectorQuery().select('#store-map').context((res) => {
			this.mapContext = res.context
		}).exec()
	},
	loadCurrentLocation() {
		wx.getLocation({
			type: 'wgs84',
			success: (res) => {
				const latitude = res.latitude
				const longitude = res.longitude
				this.setData({
					latitude,
					longitude
				})
			}
		})
	},
	goToCurrentLocation() {
		this.mapContext.moveToLocation()
	},


	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage() {

	}
})